/**
 * Created by shenhai on 15/9/2.
 */

var userService = require('../../../services/mis/system/userService');
var messageUtil = require('../../../common/utils/messageUtil');
var toolUtil = require('../../../common/utils/toolUtil');

exports.userList = function(req, res) {

    userService.userListService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when userList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when userList...'));
        }else {
            var data = {
                count:results[0]["count"],
                totalPage: results[0]["totalPage"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};


exports.userAdd = function(req, res) {

    userService.userAddService(req, function(err, results) {

        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when userAdd...'));
        }else {
            var data = {};
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};


exports.userDelete = function(req, res) {

    userService.userDeleteService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when userDelete...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when userDelete...'));
        }else {
            var data = {};
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};

exports.userEdit = function(req, res) {

    userService.userEditService(req, function(err, results) {

        if (err) {
            logger.error('Exception happen when userEdit...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when userEdit...'));
        }else {
            var data = {};
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};

exports.changePassword = function(req, res) {

    userService.changePasswordService(req, function(err, results) {

        if (err) {
            if(err.errmsg){
                res.json(messageUtil.composeFailedMessage(req,err.errmsg ));
            }else {
                logger.error('Exception happen when userEdit...' + err.message);
                res.json(messageUtil.composeFailedMessage(req, 'Exception happen when userEdit...'));
            };
        }else {
            var data = {};
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};